package org.liveboardgames.agricola.control.delegates.mock.cards
{
    import mx.collections.ArrayCollection;
    import mx.collections.ListCollectionView;
    
    import org.liveboardgames.agricola.control.delegates.mock.cards.actioncards.FamilyBoardCards;
    import org.liveboardgames.agricola.control.delegates.mock.cards.actioncards.FivePlayersFamilyCards;
    import org.liveboardgames.agricola.control.delegates.mock.cards.actioncards.FourPlayersFamilyCards;
    import org.liveboardgames.agricola.control.delegates.mock.cards.actioncards.PeriodFiveCards;
    import org.liveboardgames.agricola.control.delegates.mock.cards.actioncards.PeriodFourCards;
    import org.liveboardgames.agricola.control.delegates.mock.cards.actioncards.PeriodOneCards;
    import org.liveboardgames.agricola.control.delegates.mock.cards.actioncards.PeriodSixCards;
    import org.liveboardgames.agricola.control.delegates.mock.cards.actioncards.PeriodThreeCards;
    import org.liveboardgames.agricola.control.delegates.mock.cards.actioncards.PeriodTwoCards;
    import org.liveboardgames.agricola.control.delegates.mock.cards.actioncards.PredefinedBoardCards;
    import org.liveboardgames.agricola.control.delegates.mock.cards.actioncards.ThreePlayersFamilyCards;
    import org.liveboardgames.agricola.model.enums.GameTypeEnum;
    import org.liveboardgames.common.utils.collections.SortUtils;
    
    public class ActionCardFactory
    {
        /**
         * Return a list that contains all the action cards for the
         * left part of the board, given the number of players and the
         * version of the game (family or not).
         */
        public static function getActionCards(numberOfPlayers:int, useFamily:Boolean = true):ListCollectionView
        {
            var cards:ListCollectionView = null;
            
            cards = retrieveActionCards(numberOfPlayers, useFamily);
            
            // Make the start random
            SortUtils.doRandomSort(cards);
            
            return cards;
        }
        
        private static function retrieveActionCards(numberOfPlayers:int, useFamily:Boolean = true):ListCollectionView
        {
            
            // Build the list of cards
            var cards:ListCollectionView = new ArrayCollection();
            
            if (numberOfPlayers == 3)
            {
                // Build cards			
                cards.addItem(ThreePlayersFamilyCards.ONE_BUID_MATERIAL);
                cards.addItem(ThreePlayersFamilyCards.ONE_CLAY);
                cards.addItem(ThreePlayersFamilyCards.TWO_BUID_MATERIAL);
                cards.addItem(ThreePlayersFamilyCards.TWO_WOOD);
            }
            else if (numberOfPlayers == 4)
            {
                // Build cards			
                cards.addItem(FourPlayersFamilyCards.ONE_WOOD);
                cards.addItem(FourPlayersFamilyCards.REED_STONE_FOOD);
                cards.addItem(FourPlayersFamilyCards.TRAVELING_PLAYER);
                cards.addItem(FourPlayersFamilyCards.TWO_BUID_MATERIAL);
                cards.addItem(FourPlayersFamilyCards.TWO_CLAY);
                cards.addItem(FourPlayersFamilyCards.TWO_WOOD);
            }
            else if (numberOfPlayers == 5)
            {
                // Build cards			
                cards.addItem(FivePlayersFamilyCards.ANIMALS);
                cards.addItem(FivePlayersFamilyCards.FOUR_WOOD);
                cards.addItem(FivePlayersFamilyCards.REED_WOOD_STONE);
                cards.addItem(FivePlayersFamilyCards.THREE_CLAY);
                cards.addItem(FivePlayersFamilyCards.TRAVELING_PLAYER_OR_ROOM);
                cards.addItem(FivePlayersFamilyCards.TWO_BUILD_OR_BIRTH);
            }
            else
            {
                throw new Error("Game for " + numberOfPlayers + " not supported yet");
            }
            
            return cards;
        }
        
        public static function getRandomCardsForPeriod(period:int):ListCollectionView
        {
            var randomCards:ListCollectionView = new ArrayCollection();
            // Retrieve all the cards for the first period
            if (period == 1)
            {
                randomCards.addItem(PeriodOneCards.FENCES);
                randomCards.addItem(PeriodOneCards.IMPROVEMENT);
                randomCards.addItem(PeriodOneCards.ONE_SHEEP);
                randomCards.addItem(PeriodOneCards.SOW_ANDOR_BAKE);
            }
            else if (period == 2)
            {
                randomCards.addItem(PeriodTwoCards.BIRTH_THEN_MINOR_IMPROVEMENT);
                randomCards.addItem(PeriodTwoCards.ONE_STONE);
                randomCards.addItem(PeriodTwoCards.RENOVATION_THEN_IMPROVEMENT);
            }
            else if (period == 3)
            {
                randomCards.addItem(PeriodThreeCards.ONE_BOAR);
                randomCards.addItem(PeriodThreeCards.ONE_VEGETABLE);
            }
            else if (period == 4)
            {
                randomCards.addItem(PeriodFourCards.ONE_CATTLE);
                randomCards.addItem(PeriodFourCards.ONE_STONE);
            }
            else if (period == 5)
            {
                randomCards.addItem(PeriodFiveCards.BIRTH_WITHOUT_ROOM);
                randomCards.addItem(PeriodFiveCards.PLOUGH_ANDOR_SOW);
            }
            else if (period == 6)
            {
                randomCards.addItem(PeriodSixCards.RENOVATION_THEN_FENCES);
            }
            
            // Random sort the collection
            SortUtils.doRandomSort(randomCards);
            
            return randomCards;
        }
        
        /**
         * Returns a collection filled (in order) with the
         * actions that should match the static parts of the
         * board, i.e. the tiles with id going from 6 to 15
         * (included)?
         */
        public static function getStaticActionCards(gameType:String):ListCollectionView
        {
            var staticCards:ListCollectionView = new ArrayCollection();
            // Add the type-dependant actions
            if (gameType == GameTypeEnum.FAMILY)
            {
                staticCards.addItem(FamilyBoardCards.ROOM_ANDOR_STABLE);
                staticCards.addItem(FamilyBoardCards.FIRST_PLAYER_AND_FOOD);
                staticCards.addItem(FamilyBoardCards.ONE_GRAIN);
                staticCards.addItem(FamilyBoardCards.PLOUGH);
                staticCards.addItem(FamilyBoardCards.STABLE_ANDOR_BAKE);
                staticCards.addItem(FamilyBoardCards.DAILY_LABORER);
            }
            else
            {
            	throw new Error("Unsupported game type: " + gameType);
            }
            
            // Add the generic actions
            staticCards.addItem(PredefinedBoardCards.THREE_WOOD);
            staticCards.addItem(PredefinedBoardCards.ONE_CLAY);
            staticCards.addItem(PredefinedBoardCards.ONE_REED);
            staticCards.addItem(PredefinedBoardCards.ONE_FOOD);
            
            return staticCards;
        }
    }
}